package com.csit.action;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;

import com.csit.bean.Pager;
import com.csit.entity.Supplier;
import com.csit.service.SupplierService;
import com.csit.util.JsonAndBeanUtil;
import com.csit.util.ListAndJsonUtil;
import com.csit.util.ResponseMsgUtil;
import com.csit.util.StringUtil;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("basePackage")
public class SupplierAction extends BaseAction implements ModelDriven<Supplier> {

	private static final long serialVersionUID = -248948137332203808L;

	@Resource
	private SupplierService supplierService;
	
	private Supplier supplier = new Supplier();
	public Supplier getModel() {
		return supplier;
	}

	public void add(){
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		if(StringUtil.isNull(supplier.getSupplierCode())){
			msgUtil.setMsg("供应商编码不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		if(StringUtil.isNull(supplier.getSupplierName())){
			msgUtil.setMsg("供应商名称不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		if(supplierService.isEntityExist("supplierCode", supplier.getSupplierCode())){
			msgUtil.setMsg("该供应商编号已存在");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		supplierService.saveEntity(supplier);
		msgUtil.setSuccess(true);
		ajaxJson(msgUtil.toJSON());
	}
	
	public void update(){
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		if(StringUtil.isNull(supplier.getSupplierCode())){
			msgUtil.setMsg("供应商编码不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		if(StringUtil.isNull(supplier.getSupplierName())){
			msgUtil.setMsg("供应商名称不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		Supplier model = supplierService.getEntity(supplier.getSupplierID());
		if(!supplierService.isEntityUnique("supplierCode",model.getSupplierCode(), supplier.getSupplierCode())){
			msgUtil.setMsg("该供应商编号已存在");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		supplierService.updateEntity(supplier);
		msgUtil.setSuccess(true);
		ajaxJson(msgUtil.toJSON());
	}
	
	public void select(){
		Pager pager = new Pager(page, rows);
		String[] propertys = {"supplierCode","supplierName"};
		pager = supplierService.findByPager(pager, propertys, supplier, true);
		String[] propertiesName = {"supplierID","supplierCode","supplierName","contacter","phone","address","email","note"};
		String jsonString = ListAndJsonUtil.getJSONString(pager.getList(), propertiesName, pager.getTotalCount());
		ajaxJson(jsonString);
	}
	
	public void selectCombogrid(){
		Pager pager = new Pager(page, rows);
		String[] propertys = {"supplierCode"};
		String q = getParameter("q");
		supplier.setSupplierCode(q);
		pager = supplierService.findByPager(pager, propertys, supplier, true);
		String[] propertiesName = {"supplierID","supplierCode","supplierName"};
		String jsonString = ListAndJsonUtil.getJSONString(pager.getList(), propertiesName, pager.getTotalCount());
		ajaxJson(jsonString);
	}
	
	public void get(){
		supplier = supplierService.getEntity(supplier.getSupplierID());
		String jsonString = JsonAndBeanUtil.getJsonString(supplier);
		ajaxJson(jsonString);
	}
}
